A code segment of a large, legacy program is written by a programmer with the expectation that another portion of the program (i.e., one or more other code segments) will have established a relevant execution state for the execution of the code segment, including the inputs and addresses referenced by the instructions of the code segment. It is often useful to test a code segment of a larger program, but testing a code segment in isolation involves establishing an execution state; otherwise the code segment's output is likely to be uninteresting or cause a system crash.
Various tools have been developed to assist in the testing of code fragments or segments. For example, in order to execute and test software segments in isolation (e.g., “unit testing”), a test harness can be written to isolate the component from the other components of the program. A test harness includes code and data that emulates the larger executable environment in which the code segment is designed to operate. But manual test harness programming is time-consuming and expensive. In automatic test-harness generation, the code segment is statically analyzed in order to test its input/output interface and test harness inputs are automatically generated for the detected interface. Automatic test harness generation often requires user guidance and results in imprecise tests, and has therefore not been widely implemented.
Within object-oriented programming, mock objects for unit testing simulate the behavior of other objects that are impractical to include within the testing environment. Frameworks for mock object creation and unit testing are not automatic and require user set-up. These are implemented only for object-oriented languages, and not for legacy code, such as code written in C, C++, etc., or for large systems.